package sg.edu.nus.iss.vmcs.builder;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;

import sg.edu.nus.iss.vmcs.maintenance.ButtonItem;
import sg.edu.nus.iss.vmcs.maintenance.CoinDisplay;
import sg.edu.nus.iss.vmcs.maintenance.DrinkDisplay;
import sg.edu.nus.iss.vmcs.maintenance.ExitButtonListener;
import sg.edu.nus.iss.vmcs.maintenance.MaintenanceController;
import sg.edu.nus.iss.vmcs.maintenance.MaintenancePanel;
import sg.edu.nus.iss.vmcs.maintenance.PasswordListener;
import sg.edu.nus.iss.vmcs.maintenance.TotalCashButtonListener;
import sg.edu.nus.iss.vmcs.maintenance.TransferCashButtonListener;
import sg.edu.nus.iss.vmcs.util.LabelledDisplay;
import sg.edu.nus.iss.vmcs.util.WarningDisplay;

/**
 * @version 1.0
 * @author Yeo Hee Ling
 */
public class MaintenanceUIBuilder extends UIBuilder {
	
	private MaintenanceController maintenanceController;
	private MaintenancePanel maintPanel;
	
	public MaintenanceUIBuilder(Frame frame, MaintenanceController maintenanceController) {
		this.maintenanceController = maintenanceController;
		maintPanel = new MaintenancePanel(frame, maintenanceController);
	}
	
	public void buildVendingNorthPanel () {
		Label lb = new Label(maintPanel.getTitle());
		lb.setFont(new Font("Helvetica", Font.BOLD, 24));
		Panel tp1 = new Panel();
		tp1.add(lb);

		Panel tpn = new Panel();
		tpn.setLayout(new GridLayout(0, 1));

		LabelledDisplay password = new LabelledDisplay("Password:", 30, LabelledDisplay.FLOW);
		PasswordListener pl = new PasswordListener(maintenanceController.getAccessManager());
		password.addListener(pl);
		maintPanel.setPasswordDisplay(password);

		Panel tp3 = new Panel();
		WarningDisplay validPswd = new WarningDisplay("Valid Password");
		maintPanel.setValidPasswordWarningDisplay(validPswd);
		WarningDisplay invalidPswd = new WarningDisplay("Invalid Password");
		maintPanel.setInvalidPasswordWarningDisplay(invalidPswd);
		
		tp3.add(validPswd);
		tp3.add(invalidPswd);
		tpn.add(tp1);
		tpn.add(password);
		tpn.add(tp3);
		maintPanel.add(BorderLayout.NORTH, tpn);
	}
	
	public void buildVendingCenterPanel () {
		Panel tpc = new Panel();
		tpc.setLayout(new GridLayout(0, 1));

		CoinDisplay cDisplay = new CoinDisplay(maintenanceController);
		maintPanel.setCoinDisplay(cDisplay);
		DrinkDisplay dDisplay = new DrinkDisplay(maintenanceController);
		maintPanel.setDrinkDisplay(dDisplay);

		Panel tp5 = new Panel();
		tp5.setLayout(new GridLayout(0, 1));

		ButtonItem totalCash = new ButtonItem("Show Total Cash Held", 5, ButtonItem.FLOW);
		TotalCashButtonListener tl = new TotalCashButtonListener(maintenanceController);
		totalCash.addListener(tl);
		maintPanel.setTotalCashButtonItem(totalCash);

		Button transferCash = new Button("Press to Collect All Cash");
		transferCash.addActionListener(new TransferCashButtonListener(maintenanceController));
		maintPanel.setTransferButton(transferCash);

		Panel tp6 = new Panel();
		tp6.setLayout(new FlowLayout());
		tp6.add(transferCash);

		LabelledDisplay collectCash = new LabelledDisplay("Collect Cash:", 5, LabelledDisplay.FLOW);
		maintPanel.setCollectCashDisplay(collectCash);
		
		Button exitBtn = new Button("Press Here when Finished");
		exitBtn.addActionListener(new ExitButtonListener(maintenanceController));
		maintPanel.setExitButton(exitBtn);

		tp5.add(totalCash);
		tp5.add(tp6);
		tp5.add(collectCash);
		tp5.add(exitBtn);
		tpc.setLayout(new BorderLayout());
		Panel pp = new Panel();
		pp.setLayout(new GridLayout(1, 2));
		pp.add(cDisplay);
		pp.add(dDisplay);
		tpc.add(BorderLayout.CENTER, pp);
		tpc.add(BorderLayout.SOUTH, tp5);
		maintPanel.add(BorderLayout.CENTER, tpc);
	}
	
	public void buildVendingSouthPanel () {
		maintPanel.pack();
	}
	
	public MaintenancePanel getResultPanel() {
		return this.maintPanel;
	}
}
